import {
  FieldMetadataType,
  RelationOnDeleteAction,
  RelationType,
} from 'twenty-shared/types';

import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type AllStandardObjectFieldName } from 'src/engine/workspace-manager/twenty-standard-application/types/all-standard-object-field-name.type';
import { createStandardFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-field-flat-metadata.util';
import { createStandardRelationFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-relation-field-flat-metadata.util';
import { type StandardFieldMetadataIdByObjectAndFieldName } from 'src/engine/workspace-manager/twenty-standard-application/utils/get-standard-field-metadata-id-by-object-and-field-name.util';

export const buildConnectedAccountStandardFlatFieldMetadatas = ({
  createdAt,
  workspaceId,
  standardFieldMetadataIdByObjectAndFieldName,
}: {
  createdAt: Date;
  workspaceId: string;
  standardFieldMetadataIdByObjectAndFieldName: StandardFieldMetadataIdByObjectAndFieldName;
}): Record<
  AllStandardObjectFieldName<'connectedAccount'>,
  FlatFieldMetadata
> => ({
  id: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'id',
      type: FieldMetadataType.UUID,
      label: 'Id',
      description: 'Id',
      icon: 'Icon123',
      isSystem: true,
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'uuid',
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  createdAt: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'createdAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Creation date',
      description: 'Creation date',
      icon: 'IconCalendar',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  updatedAt: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'updatedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Last update',
      description: 'Last time the record was changed',
      icon: 'IconCalendarClock',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  deletedAt: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'deletedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Deleted at',
      description: 'Date when the record was deleted',
      icon: 'IconCalendarMinus',
      isNullable: true,
      isUIReadOnly: true,
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  handle: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'handle',
      type: FieldMetadataType.TEXT,
      label: 'handle',
      description: 'The account handle (email, username, phone number, etc.)',
      icon: 'IconMail',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  provider: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'provider',
      type: FieldMetadataType.TEXT,
      label: 'provider',
      description: 'The account provider',
      icon: 'IconSettings',
      isNullable: false,
      defaultValue: "'google'",
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  accessToken: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'accessToken',
      type: FieldMetadataType.TEXT,
      label: 'Access Token',
      description: 'Messaging provider access token',
      icon: 'IconKey',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  refreshToken: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'refreshToken',
      type: FieldMetadataType.TEXT,
      label: 'Refresh Token',
      description: 'Messaging provider refresh token',
      icon: 'IconKey',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  lastSyncHistoryId: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'lastSyncHistoryId',
      type: FieldMetadataType.TEXT,
      label: 'Last sync history ID',
      description: 'Last sync history ID',
      icon: 'IconHistory',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  authFailedAt: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'authFailedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Auth failed at',
      description: 'Auth failed at',
      icon: 'IconX',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  lastCredentialsRefreshedAt: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'lastCredentialsRefreshedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Last credentials refreshed at',
      description: 'Last credentials refreshed at',
      icon: 'IconHistory',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  handleAliases: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'handleAliases',
      type: FieldMetadataType.TEXT,
      label: 'Handle Aliases',
      description: 'Handle Aliases',
      icon: 'IconMail',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  scopes: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'scopes',
      type: FieldMetadataType.ARRAY,
      label: 'Scopes',
      description: 'Scopes',
      icon: 'IconSettings',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  connectionParameters: createStandardFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'connectionParameters',
      type: FieldMetadataType.RAW_JSON,
      label: 'Custom Connection Parameters',
      description: 'JSON object containing custom connection parameters',
      icon: 'IconSettings',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  accountOwner: createStandardRelationFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'accountOwner',
      label: 'Account Owner',
      description: 'Account Owner',
      icon: 'IconUserCircle',
      isNullable: false,
      createdAt,
      targetObjectName: 'workspaceMember',
      targetFieldName: 'connectedAccounts',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.CASCADE,
        joinColumnName: 'accountOwnerId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  messageChannels: createStandardRelationFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'messageChannels',
      label: 'Message Channels',
      description: 'Message Channels',
      icon: 'IconMessage',
      isNullable: false,
      createdAt,
      targetObjectName: 'messageChannel',
      targetFieldName: 'connectedAccount',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  calendarChannels: createStandardRelationFieldFlatMetadata({
    objectName: 'connectedAccount',
    workspaceId,
    options: {
      fieldName: 'calendarChannels',
      label: 'Calendar Channels',
      description: 'Calendar Channels',
      icon: 'IconCalendar',
      isNullable: false,
      createdAt,
      targetObjectName: 'calendarChannel',
      targetFieldName: 'connectedAccount',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
});
